@@ -29,6 +29,11 @@ def inbox(request):
29
29
30
30
messages = paginate (request , messages , per_page = MESSAGES_PER_PAGE , count = count )
31
31
32
+ # Add is_bot_user flag to each message's sender
33
+ for message in messages .object_list :
34
+ if message .sender :
35
+ message .sender .is_bot_user = message .sender .username == settings .SUMO_BOT_USERNAME
36
+
32
37
return render (
33
38
request ,
34
39
"messages/inbox.html" ,
@@ -42,25 +47,38 @@ def read(request, msgid):
42
47
was_new = message .unread
43
48
if was_new :
44
49
message .update (read = True )
50
+ if message .sender .username == settings .SUMO_BOT_USERNAME :
51
+ message .sender .is_bot_user = True
52
+
45
53
initial = {"to" : message .sender , "in_reply_to" : message .pk }
46
54
form = ReplyForm (initial = initial )
47
55
response = render (
48
56
request ,
49
57
"messages/read.html" ,
50
- {"message" : message , "form" : form , "default_avatar" : settings .DEFAULT_AVATAR },
58
+ {
59
+ "message" : message ,
60
+ "form" : form ,
61
+ "default_avatar" : settings .DEFAULT_AVATAR ,
62
+ },
51
63
)
52
64
return response
53
65
54
66
55
67
@login_required
56
68
def read_outbox (request , msgid ):
57
69
message = get_object_or_404 (OutboxMessage , pk = msgid , sender = request .user )
70
+ to_users = message .to .all ()
71
+
72
+ # Add is_bot_user flag to each recipient
73
+ for user in to_users :
74
+ user .is_bot_user = user .username == settings .SUMO_BOT_USERNAME
75
+
58
76
return render (
59
77
request ,
60
78
"messages/read-outbox.html" ,
61
79
{
62
80
"message" : _add_recipients (message ),
63
- "to_users" : message . to . all () ,
81
+ "to_users" : to_users ,
64
82
"to_groups" : message .to_group .all (),
65
83
},
66
84
)
@@ -179,13 +197,37 @@ def preview_async(request):
179
197
180
198
181
199
def _add_recipients (msg ):
200
+ """Process and attach recipient information to a message object.
201
+
202
+ This helper function calculates recipient counts and assigns recipient-related
203
+ attributes to the message object for both individual users and groups.
204
+
205
+ Args:
206
+ msg: An OutboxMessage object to process.
207
+
208
+ Returns:
209
+ The modified message object with the following attributes set:
210
+ - recipients_count: Number of individual recipients
211
+ - to_groups_count: Number of group recipients
212
+ - recipient: The single recipient (if exactly one), else None
213
+ - recipient.is_bot_user: Boolean indicating if recipient is SumoBot
214
+ - to_groups: List of recipient groups with prefetched profiles
215
+
216
+ Note:
217
+ The function assumes msg.to and msg.to_group are valid related fields
218
+ on the message object.
219
+ """
182
220
# Set the counts based on the lists
183
221
msg .recipients_count = msg .to .all ().count ()
184
222
msg .to_groups_count = msg .to_group .all ().count ()
185
223
186
224
# Assign the recipient based on the number of recipients
187
225
msg .recipient = msg .to .all ()[0 ] if msg .recipients_count == 1 else None
188
226
227
+ # Set is_bot_user flag only if there's a recipient with a username
228
+ if msg .recipient and hasattr (msg .recipient , "username" ):
229
+ msg .recipient .is_bot_user = msg .recipient .username == settings .SUMO_BOT_USERNAME
230
+
189
231
# Assign the group(s) based on the number of groups
190
232
msg .to_groups = list (msg .to_group .prefetch_related ("profile" ))
191
233
0 commit comments